//package com.xiaocikuai.smallchunks.controller.User;
//
//
//import com.xiaocikuai.smallchunks.entity.User.Duser;
//import com.xiaocikuai.smallchunks.entity.User.Membership;
//import com.xiaocikuai.smallchunks.service.User.impl.MembershipServiceImpl;
//import com.xiaocikuai.smallchunks.utils.resultd.Resultd;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//
//import java.util.List;
//
///**
// * <p>
// *  前端控制器
// * </p>
// *
// * @author author1
// * @since 2024-07-14
// */
//@RestController
//@RequestMapping("/membership")
//public class MembershipController {
//
//    @Autowired
//    private MembershipServiceImpl membershipService;
//
//
//    @PostMapping("/update")
//    public Resultd updateDuser(@RequestHeader("Authorization") String token, @RequestBody Membership membership) {
//        if (membershipService.updateById(membership)) {
//            return Resultd.ok().data("info", "更新成功");
//        } else {
//            return Resultd.error().data("info", "更新失败");
//        }
//    }
//
//
//    @PostMapping("/selectinfo")
//    public Resultd selectList(@RequestHeader("Authorization") String token, @RequestBody Membership membership) {
//        List<Membership> dusers = membershipService.list();
//        if (dusers!= null) {
//            return Resultd.ok().data("info", dusers);
//        } else {
//            return Resultd.error().data("info", "未找到对应记录");
//        }
//    }
//
//    @PostMapping("updateList")
//    public Resultd updateList(@RequestHeader("Authorization") String token, @RequestBody List<Membership> memberships) {
//        if (membershipService.updateBatchById(memberships)) {
//            return Resultd.ok().data("info", "更新成功");
//        } else {
//            return Resultd.error().data("info", "更新失败");
//        }
//    }
//
//    @PostMapping("selectById")
//    public Resultd selectById(@RequestHeader("Authorization") String token, @RequestBody Membership membership) {
//        Membership membership1 = membershipService.getById(membership.getMid());
//        if (membership1 != null) {
//            return Resultd.ok().data("info", membership1);
//        } else {
//            return Resultd.error().data("info", "更新失败");
//        }
//    }
//
//
//
//
//}
package com.xiaocikuai.smallchunks.controller.User;

import com.xiaocikuai.smallchunks.entity.User.Duser;
import com.xiaocikuai.smallchunks.entity.User.Membership;
import com.xiaocikuai.smallchunks.service.User.impl.MembershipServiceImpl;
import com.xiaocikuai.smallchunks.utils.resultd.Resultd;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author1
 * @since 2024-07-14
 */
@Api(tags = "会员控制器")
@RestController
@RequestMapping("/api/membership")
public class MembershipController {

    @Autowired
    private MembershipServiceImpl membershipService;

    @ApiOperation(value = "更新会员信息")
    @PostMapping("/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "membership", value = "会员信息", required = true, dataType = "Membership", paramType = "body")
    })
    public Resultd updateDuser(@RequestHeader("Authorization") String token, @RequestBody Membership membership) {
        if (membershipService.updateById(membership)) {
            return Resultd.ok().data("info", "更新成功");
        } else {
            return Resultd.error().data("info", "更新失败");
        }
    }

    @ApiOperation(value = "查询会员列表")
    @PostMapping("/selectinfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "membership", value = "会员信息（用于筛选）", required = true, dataType = "Membership", paramType = "body")
    })
    public Resultd selectList(@RequestHeader("Authorization") String token, @RequestBody Membership membership) {
        List<Membership> dusers = membershipService.list();
        if (dusers!= null) {
            return Resultd.ok().data("info", dusers);
        } else {
            return Resultd.error().data("info", "未找到对应记录");
        }
    }

    @ApiOperation(value = "批量更新会员列表")
    @PostMapping("/updateList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "memberships", value = "会员列表", required = true, dataType = "List<Membership>", paramType = "body")
    })
    public Resultd updateList(@RequestHeader("Authorization") String token, @RequestBody List<Membership> memberships) {
        if (membershipService.updateBatchById(memberships)) {
            return Resultd.ok().data("info", "更新成功");
        } else {
            return Resultd.error().data("info", "更新失败");
        }
    }

    @ApiOperation(value = "根据 ID 查询会员信息")
    @PostMapping("/selectById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "membership", value = "会员信息（包含 ID）", required = true, dataType = "Membership", paramType = "body")
    })
    public Resultd selectById(@RequestHeader("Authorization") String token, @RequestBody Membership membership) {
        Membership membership1 = membershipService.getById(membership.getMid());
        if (membership1!= null) {
            return Resultd.ok().data("info", membership1);
        } else {
            return Resultd.error().data("info", "更新失败");
        }
    }


}